<template>
  <div class="page_vue">
    <div class="page_header">
      <div class="header_left">
        <el-divider direction="vertical" class="header_divider"></el-divider>
        <span class="header_title">{{ $t("home.myDocuments") }}</span>
        <div class="header_refresh">
          <img src="../../../assets/images/home/refresh.png" class="header_refresh_img" alt="" @click="refreshClick" />
        </div>
      </div>
      <div class="header_right">
        <el-tabs v-model="activeName" @tab-click="refreshClick">
          <!-- <el-tab-pane :label="$t('home.nearest')" name="nearest"></el-tab-pane> -->
          <el-tab-pane :label="$t('home.distributive')" name="handOut"></el-tab-pane>
          <el-tab-pane :label="$t('home.receiving')" name="receive"></el-tab-pane>
          <el-tab-pane :label="$t('home.shared')" name="share"></el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <div class="page_body">
      <el-table :data="tableData" style="width: 100%" height="240" v-loading="tableLoading"
        :header-cell-style="headerCellStyle" @row-dblclick="tableDataRowDblclick">
        <el-table-column :label="activeName == 'share' ? $t('home.linkName') : $t('home.documentName')"
          show-overflow-tooltip>
          <template #default="scope">
            <span class="left_dot"></span>
            <span v-if="activeName == 'nearest'">{{ scope.row.realName }}{{ scope.row.suffix ? "." + scope.row.suffix :
              ""
              }}</span>
            <span v-if="activeName == 'handOut' || activeName == 'receive'">{{ scope.row.fileName }}</span>
            <span v-if="activeName == 'share'">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="activeName == 'handOut' || activeName == 'receive' ? $t('home.distributor') : $t('home.userName')">
          <template #default="scope">
            <div v-if="activeName == 'handOut' || activeName == 'receive'" class="right_user">
              <div class="nicknameBg">{{ scope.row.distributorName ? scope.row.distributorName.charAt(0) : "" }}</div>
              <span>{{ scope.row.distributorName }}</span>
            </div>
            <div v-else class="right_user">
              <div class="nicknameBg">{{ userStore.nickname.charAt(0) }}</div>
              <span>{{ userStore.nickname }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('home.source')">
          <template #default="scope">
            <div v-if="activeName == 'nearest'">
              {{ scope.row.libraryType == "personal" ? $t('home.personalDocuments') : $t('home.teamDocumentation') }}
            </div>
            <div v-if="activeName == 'handOut' || activeName == 'receive'">
              {{ scope.row.type == "0" ? $t('home.personalDocuments') : $t('home.teamDocumentation') }}
            </div>
            <div v-if="activeName == 'share'">
              {{ scope.row.shareType == "personal" ? $t('home.personalDocuments') : $t('home.teamDocumentation') }}
            </div>
          </template>
        </el-table-column>

        <el-table-column :label="$t('home.updateTime')" align="center" width="130">
          <template #default="scope">
            <span v-if="activeName == 'nearest'">{{ proxy.$moment(scope.row.viewedTime).format("MM月DD日 HH:mm ")
              }}</span>
            <span v-if="activeName == 'handOut' || activeName == 'receive'">{{
              proxy.$moment(scope.row.distributeTime).format("MM月DD日 HH:mm") }}</span>
            <span v-if="activeName == 'share'">{{ scope.row.updateTime ? proxy.$moment(scope.row.updateTime).format("MM月DD日 HH:mm"):'-' }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page_footer">
      <el-pagination background size="small" @current-change="handleCurrentChange" layout="total, prev, pager, next"
        v-model:current-page="requestParams.page" v-model:page-size="requestParams.size" :total="requestParams.total">
      </el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import homeIndexApi from '@/api/homeIndex/homeIndexApi';
import useUserStore from '@/store/modules/user';

const { proxy } = getCurrentInstance() as any;
const router = useRouter();
const userStore: any = useUserStore();

const tableLoading = ref(false);
const activeName = ref("handOut")
const tableData = ref([])
const requestParams = ref({
  page: 1,
  size: 5,
  total: 0
})

const handleClick = async () => {
  let res;
  tableLoading.value = true;
  let params: any = {
    page: requestParams.value.page-1,
    size: requestParams.value.size
  };
  try {
    if (activeName.value == "nearest") {
      params.libraryType = "personal"
      // res = await homeMyDocumentApi.apiFilesRecentlyViewedQuery(params);
      // tableData.value = res.rows;
      // requestParams.value.total = res.totalCount;
    }
    if (activeName.value == "handOut") {
      //分发
      params.modules = "personal";
      res = await homeIndexApi.ApiPerQueryDistribute(params);
      tableData.value = res.data.rows;
      requestParams.value.total = res.data.total;
    }
    if (activeName.value == "receive") {
      params.modules = "personal";
      res = await homeIndexApi.ApiPerQuerySend(params);
      tableData.value = res.data.rows;
      requestParams.value.total =  res.data.total;
    }
    if (activeName.value == "share") {
      params.type = "personal";
      params.userId = userStore.userId;
      res = await homeIndexApi.ApiPerSharePerson(params);
      tableData.value = res.rows;
      requestParams.value.total =  res.total;
    }
    tableLoading.value = false;
  } catch (error) {
    tableLoading.value = false;
  }
}

// 表格双击事件
const tableDataRowDblclick = (row: any) => {
  if (activeName.value == "nearest") {
    row.id = row.fileId;
    if (row.type == "file") {
      // 文件预览跳转
    }
  }
  if (activeName.value == "handOut") {
    router.push("/newUi/networkDisk/distribution-reception");
  }
  if (activeName.value == "receive") {
    router.push("/newUi/networkDisk/distribution-reception");
  }
  if (activeName.value == "share") {
    router.push("/newUi/networkDisk/share-manage");
  }
}
// 刷新
const refreshClick = (tab) => {
  activeName.value = tab.paneName;
  requestParams.value.page = 1;
  handleClick();
}
//表头行的 style 的回调方法
const headerCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  let base = {
    color: "#555555",
    "font-weight": "400",
    "font-size": "13px"
  };
  return base;
}
// 分页
const handleCurrentChange = (val: any) => {
  requestParams.value.page = val;
  handleClick();
}

onMounted(() => {
  handleClick();
});
</script>


<style scoped lang='scss'>
.page_vue {
  .page_header {
    display: flex;
    align-items: flex-start;

    .header_left {
      display: flex;
      align-items: center;

      .header_divider {
        width: 3px;
        border-radius: 2px;
        background-color: #2974f9;
      }

      .header_title {
        font-size: 16px;
        color: #2d2d2d;
        font-family: Source Han Sans CN;
        font-weight: 600;
      }

      .header_refresh {
        width: 18px;
        height: 18px;
        margin-left: 9px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 3px;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          background-color: #ececec;
        }

        .header_refresh_img {
          width: 12px;
          height: 12px;
        }
      }
    }

    .header_right {
      margin-left: 35px;
      flex: 1;
      padding-left: 20px;

      :deep(.el-tabs) {
        margin-bottom: 0;
      }

      :deep(.el-tabs__header) {
        margin-bottom: 0;
      }

      :deep(.el-tabs__item) {
        height: 24px;
        font-size: 14px;
        line-height: 18px;
      }

      :deep(.el-tabs__nav-wrap::after) {
        height: 0;
      }

      :deep(.el-tabs__active-bar) {
        width: 21px !important;
        bottom: 1px;
        left: 11px;
        background-color: #0a6cff;
      }

      :deep(.el-tabs__item.is-active) {
        color: #0a6cff;
      }
    }
  }

  .page_body {
    margin-top: 4px;

    .el-table :deep(td.el-table__cell) {
      color: #060606;
      font-size: 12px;
      border-color: #e3e3e3;
    }

    .left_dot {
      display: inline-block;
      width: 5px;
      height: 5px;
      background: #ccc;
      margin-right: 6px;
    }

    .right_user {
      display: flex;
      align-items: center;

      .nicknameBg {
        width: 16px;
        height: 16px;
        border-radius: 8px;
        background-color: #ff8622;
        text-align: center;
        line-height: 16px;
        color: #ffffff;
        margin-right: 2px;
      }
    }
  }

  .page_footer {
    margin-top: 10px;
    text-align: center;

    :deep(.el-pager li),
    :deep(.btn-prev),
    :deep(.btn-next) {
      background: #fff;
      border: 1px solid #dcdee0;
      border-radius: 3px;
      line-height: 21px;
      color: #323233;
      font-weight: 400;
    }
    :deep(.el-pager li.is-active){
      background-color: #1989fa;
      color: #ffffff;
    }
    :deep(.el-pagination__total) {
      color: #323233;
    }
  }
}
</style>